// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.characters;

import java.util.ArrayList;
import java.util.Arrays;
import se.krka.kahlua.j2se.KahluaTableImpl;
import se.krka.kahlua.vm.KahluaTableIterator;
import zombie.Lua.LuaManager;
import zombie.core.ImmutableColor;
import zombie.core.skinnedmodel.population.BeardStyle;
import zombie.core.skinnedmodel.population.HairStyle;
import zombie.core.skinnedmodel.population.OutfitRNG;
import zombie.iso.IsoWorld;
import zombie.util.StringUtils;
import zombie.util.Type;

public final class HairOutfitDefinitions {
    public static final HairOutfitDefinitions instance = new HairOutfitDefinitions();
    public boolean m_dirty = true;
    public String hairStyle;
    public int minWorldAge;
    public final ArrayList<HairOutfitDefinitions.HaircutDefinition> m_haircutDefinition = new ArrayList();
    public final ArrayList<HairOutfitDefinitions.HaircutOutfitDefinition> m_outfitDefinition = new ArrayList();
    private final ThreadLocal<ArrayList<HairStyle>> m_tempHairStyles = ThreadLocal.withInitial(ArrayList::new);

    public void checkDirty() {
        if (this.m_dirty) {
            this.m_dirty = false;
            this.init();
        }
    }

    private void init() {
        this.m_haircutDefinition.clear();
        this.m_outfitDefinition.clear();
        KahluaTableImpl kahluaTableImpl0 = (KahluaTableImpl)LuaManager.env.rawget("HairOutfitDefinitions");
        if (kahluaTableImpl0 != null) {
            KahluaTableImpl kahluaTableImpl1 = Type.tryCastTo(kahluaTableImpl0.rawget("haircutDefinition"), KahluaTableImpl.class);
            if (kahluaTableImpl1 != null) {
                KahluaTableIterator kahluaTableIterator = kahluaTableImpl1.iterator();

                while (kahluaTableIterator.advance()) {
                    KahluaTableImpl kahluaTableImpl2 = Type.tryCastTo(kahluaTableIterator.getValue(), KahluaTableImpl.class);
                    if (kahluaTableImpl2 != null) {
                        HairOutfitDefinitions.HaircutDefinition haircutDefinition = new HairOutfitDefinitions.HaircutDefinition(
                            kahluaTableImpl2.rawgetStr("name"),
                            kahluaTableImpl2.rawgetInt("minWorldAge"),
                            new ArrayList(Arrays.asList(kahluaTableImpl2.rawgetStr("onlyFor").split(",")))
                        );
                        this.m_haircutDefinition.add(haircutDefinition);
                    }
                }

                KahluaTableImpl kahluaTableImpl3 = Type.tryCastTo(kahluaTableImpl0.rawget("haircutOutfitDefinition"), KahluaTableImpl.class);
                if (kahluaTableImpl3 != null) {
                    kahluaTableIterator = kahluaTableImpl3.iterator();

                    while (kahluaTableIterator.advance()) {
                        KahluaTableImpl kahluaTableImpl4 = Type.tryCastTo(kahluaTableIterator.getValue(), KahluaTableImpl.class);
                        if (kahluaTableImpl4 != null) {
                            HairOutfitDefinitions.HaircutOutfitDefinition haircutOutfitDefinition = new HairOutfitDefinitions.HaircutOutfitDefinition(
                                kahluaTableImpl4.rawgetStr("outfit"),
                                initStringChance(kahluaTableImpl4.rawgetStr("haircut")),
                                initStringChance(kahluaTableImpl4.rawgetStr("beard")),
                                initStringChance(kahluaTableImpl4.rawgetStr("haircutColor"))
                            );
                            this.m_outfitDefinition.add(haircutOutfitDefinition);
                        }
                    }
                }
            }
        }
    }

    public boolean isHaircutValid(String outfit, String haircut) {
        instance.checkDirty();
        if (StringUtils.isNullOrEmpty(outfit)) {
            return true;
        } else {
            for (int _int = 0; _int < instance.m_haircutDefinition.size(); _int++) {
                HairOutfitDefinitions.HaircutDefinition haircutDefinition = (HairOutfitDefinitions.HaircutDefinition)instance.m_haircutDefinition.get(_int);
                if (haircutDefinition.hairStyle.equals(haircut)) {
                    if (!haircutDefinition.onlyFor.contains(outfit)) {
                        return false;
                    }

                    if (IsoWorld.instance.getWorldAgeDays() < (float)haircutDefinition.minWorldAge) {
                        return false;
                    }
                }
            }

            return true;
        }
    }

    public void getValidHairStylesForOutfit(String outfit, ArrayList<HairStyle> hairStyles, ArrayList<HairStyle> result) {
        result.clear();

        for (int _int = 0; _int < hairStyles.size(); _int++) {
            HairStyle hairStylex = (HairStyle)hairStyles.get(_int);
            if (!hairStylex.isNoChoose() && this.isHaircutValid(outfit, hairStylex.name)) {
                result.add(hairStylex);
            }
        }
    }

    public String getRandomHaircut(String outfit, ArrayList<HairStyle> hairList) {
        ArrayList arrayList = (ArrayList)this.m_tempHairStyles.get();
        this.getValidHairStylesForOutfit(outfit, hairList, arrayList);
        if (arrayList.isEmpty()) {
            return "";
        } else {
            String string = OutfitRNG.pickRandom(arrayList).name;
            boolean _boolean = false;

            for (int int0 = 0; int0 < instance.m_outfitDefinition.size() && !_boolean; int0++) {
                HairOutfitDefinitions.HaircutOutfitDefinition haircutOutfitDefinition = (HairOutfitDefinitions.HaircutOutfitDefinition)instance.m_outfitDefinition
                    .get(int0);
                if (haircutOutfitDefinition.outfit.equals(outfit) && haircutOutfitDefinition.haircutChance != null) {
                    float float0 = OutfitRNG.Next(0.0F, 100.0F);
                    float float1 = 0.0F;

                    for (int int1 = 0; int1 < haircutOutfitDefinition.haircutChance.size(); int1++) {
                        HairOutfitDefinitions.StringChance stringChance = (HairOutfitDefinitions.StringChance)haircutOutfitDefinition.haircutChance.get(int1);
                        float1 += stringChance.chance;
                        if (float0 < float1) {
                            string = stringChance.str;
                            if ("null".equalsIgnoreCase(stringChance.str)) {
                                string = "";
                            }

                            if ("random".equalsIgnoreCase(stringChance.str)) {
                                string = OutfitRNG.pickRandom(arrayList).name;
                            }

                            _boolean = true;
                            break;
                        }
                    }
                }
            }

            return string;
        }
    }

    public ImmutableColor getRandomHaircutColor(String outfit) {
        ImmutableColor immutableColor = (ImmutableColor)SurvivorDesc.HairCommonColors.get(OutfitRNG.Next(SurvivorDesc.HairCommonColors.size()));
        String string0 = null;
        boolean _boolean = false;

        for (int int0 = 0; int0 < instance.m_outfitDefinition.size() && !_boolean; int0++) {
            HairOutfitDefinitions.HaircutOutfitDefinition haircutOutfitDefinition = (HairOutfitDefinitions.HaircutOutfitDefinition)instance.m_outfitDefinition
                .get(int0);
            if (haircutOutfitDefinition.outfit.equals(outfit) && haircutOutfitDefinition.haircutColor != null) {
                float float0 = OutfitRNG.Next(0.0F, 100.0F);
                float float1 = 0.0F;

                for (int int1 = 0; int1 < haircutOutfitDefinition.haircutColor.size(); int1++) {
                    HairOutfitDefinitions.StringChance stringChance = (HairOutfitDefinitions.StringChance)haircutOutfitDefinition.haircutColor.get(int1);
                    float1 += stringChance.chance;
                    if (float0 < float1) {
                        string0 = stringChance.str;
                        if ("random".equalsIgnoreCase(stringChance.str)) {
                            immutableColor = (ImmutableColor)SurvivorDesc.HairCommonColors.get(OutfitRNG.Next(SurvivorDesc.HairCommonColors.size()));
                            string0 = null;
                        }

                        _boolean = true;
                        break;
                    }
                }
            }
        }

        if (!StringUtils.isNullOrEmpty(string0)) {
            String[] string1 = string0.split(",");
            immutableColor = new ImmutableColor(Float.parseFloat(string1[0]), Float.parseFloat(string1[1]), Float.parseFloat(string1[2]));
        }

        return immutableColor;
    }

    public String getRandomBeard(String outfit, ArrayList<BeardStyle> beardList) {
        String string = OutfitRNG.pickRandom(beardList).name;
        boolean _boolean = false;

        for (int int0 = 0; int0 < instance.m_outfitDefinition.size() && !_boolean; int0++) {
            HairOutfitDefinitions.HaircutOutfitDefinition haircutOutfitDefinition = (HairOutfitDefinitions.HaircutOutfitDefinition)instance.m_outfitDefinition
                .get(int0);
            if (haircutOutfitDefinition.outfit.equals(outfit) && haircutOutfitDefinition.beardChance != null) {
                float float0 = OutfitRNG.Next(0.0F, 100.0F);
                float float1 = 0.0F;

                for (int int1 = 0; int1 < haircutOutfitDefinition.beardChance.size(); int1++) {
                    HairOutfitDefinitions.StringChance stringChance = (HairOutfitDefinitions.StringChance)haircutOutfitDefinition.beardChance.get(int1);
                    float1 += stringChance.chance;
                    if (float0 < float1) {
                        string = stringChance.str;
                        if ("null".equalsIgnoreCase(stringChance.str)) {
                            string = "";
                        }

                        if ("random".equalsIgnoreCase(stringChance.str)) {
                            string = OutfitRNG.pickRandom(beardList).name;
                        }

                        _boolean = true;
                        break;
                    }
                }
            }
        }

        return string;
    }

    private static ArrayList<HairOutfitDefinitions.StringChance> initStringChance(String string0) {
        if (StringUtils.isNullOrWhitespace(string0)) {
            return null;
        } else {
            ArrayList arrayList = new ArrayList();
            String[] string1 = string0.split(";");
            int _int = 0;

            for (String string2 : string1) {
                String[] string3 = string2.split(":");
                HairOutfitDefinitions.StringChance stringChance0 = new HairOutfitDefinitions.StringChance();
                stringChance0.str = string3[0];
                stringChance0.chance = Float.parseFloat(string3[1]);
                _int = (int)((float)_int + stringChance0.chance);
                arrayList.add(stringChance0);
            }

            if (_int < 100) {
                HairOutfitDefinitions.StringChance stringChance1 = new HairOutfitDefinitions.StringChance();
                stringChance1.str = "random";
                stringChance1.chance = (float)(100 - _int);
                arrayList.add(stringChance1);
            }

            return arrayList;
        }
    }

    public static final class HaircutDefinition {
        public String hairStyle;
        public int minWorldAge;
        public ArrayList<String> onlyFor;

        public HaircutDefinition(String _hairStyle, int _minWorldAge, ArrayList<String> _onlyFor) {
            this.hairStyle = _hairStyle;
            this.minWorldAge = _minWorldAge;
            this.onlyFor = _onlyFor;
        }
    }

    public static final class HaircutOutfitDefinition {
        public String outfit;
        public ArrayList<HairOutfitDefinitions.StringChance> haircutChance;
        public ArrayList<HairOutfitDefinitions.StringChance> beardChance;
        public ArrayList<HairOutfitDefinitions.StringChance> haircutColor;

        public HaircutOutfitDefinition(
            String _outfit,
            ArrayList<HairOutfitDefinitions.StringChance> _haircutChance,
            ArrayList<HairOutfitDefinitions.StringChance> _beardChance,
            ArrayList<HairOutfitDefinitions.StringChance> _haircutColor
        ) {
            this.outfit = _outfit;
            this.haircutChance = _haircutChance;
            this.beardChance = _beardChance;
            this.haircutColor = _haircutColor;
        }
    }

    private static final class StringChance {
        String str;
        float chance;
    }
}
